package com.feiwo.nonego.rpc.consumer.common.context;

import com.feiwo.nonego.rpc.proxy.api.future.RpcFuture;

import java.util.Objects;
import java.util.Optional;

/**
 *
 *
 * @author feiwo 2025-08-08
 */
public class RpcContext {

    private RpcContext(){}

    private static final RpcContext INSTANCE = new RpcContext();

    private static final InheritableThreadLocal<RpcFuture> FUTURE_INHERITABLE_THREAD_LOCAL = new InheritableThreadLocal<>();

    public static RpcContext getContext() {
        return INSTANCE;
    }

    public void setFuture(RpcFuture future) {
        FUTURE_INHERITABLE_THREAD_LOCAL.set(future);
    }

    public Optional<RpcFuture> getFuture() {
        return Objects.isNull(FUTURE_INHERITABLE_THREAD_LOCAL.get()) ? Optional.empty() : Optional.of(FUTURE_INHERITABLE_THREAD_LOCAL.get());
    }

    public void clear() {
        FUTURE_INHERITABLE_THREAD_LOCAL.remove();
    }
}
